package com.dbq.fast.business.me.api;

import android.util.Log;

import com.dbq.fast.App;
import com.dbq.fast.analysis.EventConstant;
import com.dbq.fast.analysis.YibaAnalysis;
import com.dbq.fast.business.login.api.LoginApi;
import com.dbq.fast.business.me.bean.InviteFriendsInfoBean;
import com.dbq.fast.business.me.bean.InviteFriendssSubmitBean;
import com.dbq.fast.business.product.api.CommonTransformer;
import com.dbq.fast.common.bean.User;
import com.dbq.fast.common.net.HttpManager;
import com.dbq.fast.common.net.RemoteApiCallBack;

import java.util.HashMap;

/**
 * 分享模块所有的网络请求
 * Created by Administrator on 2017/12/29 0029.
 */

public class ShareApi {
    /***
     * 获取分享UI VO object
     * @param callback
     */
    public static void questService(RemoteApiCallBack callback) {
        User user = User.getInstance();
        HttpManager.getInstance().getHttpService()
                .getInviteFriendsInfo(user.getToken())
                .compose(new CommonTransformer<>())
                .subscribe(httpResult -> {
                    int code = httpResult.getCode();
                    if (code == 200) {
                        InviteFriendsInfoBean inviteFriendsInfoBean = httpResult.getData();
                        if (inviteFriendsInfoBean != null) {
                            callback.onSuccess(inviteFriendsInfoBean);
                        }
                    } else if (code == 401) {
                        LoginApi.refreshToken(true, new RemoteApiCallBack<Boolean>() {
                            @Override
                            public void onSuccess(Boolean aBoolean) {
                                questService(callback);
                            }

                            @Override
                            public void onFail(int status, String message) {
                                callback.onFail(404, "");
                            }
                        });
                    } else if (code == 402) {
                        callback.onFail(code, ""/*App.getContext().getString(R.string.login_on_other_device)*/);
                    }else {
                        callback.onFail(httpResult.getCode(), httpResult.getMsg());
                    }
                }, throwable -> {
                    Log.i("throwable","throwable  = "+ throwable.toString());
                    callback.onFail(404, "");
                });
    }

    /**
     * 分享邀请码页面的提交事件，用于检测用户是否已经绑定过邀请码
     * @param callback
     */
    public static void submitShareCode(RemoteApiCallBack callback,String vpnId) {
        User user = User.getInstance();
        HashMap hashMap = new HashMap();
        hashMap.put("vpnId",vpnId);
        HttpManager.getInstance().getHttpService()
                .doInviteFriends(user.getToken(),hashMap)
                .compose(new CommonTransformer<>())
                .subscribe(httpResult -> {
                    int code = httpResult.getCode();
                    if (code == 200) {
                        YibaAnalysis.getInstance().event(App.getContext(), EventConstant. SUBMIT_CODE_SUCCESS);
                        InviteFriendssSubmitBean amount = httpResult.getData();
                        if (amount != null) {
                            callback.onSuccess(amount);
                        }
                    } else if (code == 401) {
                        LoginApi.refreshToken(true, new RemoteApiCallBack<Boolean>() {
                            @Override
                            public void onSuccess(Boolean aBoolean) {
                                questService(callback);
                            }

                            @Override
                            public void onFail(int status, String message) {
                                callback.onFail(404, "");
                            }
                        });
                    } else if (code == 402) {
                        callback.onFail(code, ""/*App.getContext().getString(R.string.login_on_other_device)*/);
                    }else if(code == -1008){ //邀请码无效
                        YibaAnalysis.getInstance().event(App.getContext(), EventConstant. SUBMIT_CODE_FAIL_INVALID);
                        callback.onFail(httpResult.getCode(), httpResult.getMsg());
                    }else if(code == -1009){//-1009 包含时间过早
                        callback.onFail(httpResult.getCode(), httpResult.getMsg());
                        YibaAnalysis.getInstance().event(App.getContext(), EventConstant. SUBMIT_CODE_FAIL_TIME);
                    }
                }, throwable -> {
                    Log.i("throwable","throwable  = "+ throwable.toString());
                    YibaAnalysis.getInstance().event(App.getContext(), EventConstant. SUBMIT_CODE_FAIL_NETWORK);
                    callback.onFail(404, "");
                });
    }


}
